//
//  PiPiBirdTabBarController.m
//  pipiNiao_user
//
//  Created by 黄军 on 2017/6/19.
//  Copyright © 2017年 $. All rights reserved.
//

#import "AoShuTabBarController.h"

#import "RootViewController.h"
#import "FaxianViewController.h"
#import "ZiXunViewController.h"
#import "hangQingViewController.h"

static NSInteger selectNumber;

@interface AoShuTabBarController ()<UITabBarDelegate,UIWebViewDelegate>

@property(nonatomic,strong)UIWebView *webView;
@property(nonatomic,strong) NSDictionary *dic;


#define RGB(r,g,b) [UIColor colorWithRed:((float)(r))/255.0 green:((float)(g))/255.0 blue:((float)(b))/255.0 alpha:1.0]

@end

@implementation AoShuTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tabBar.tintColor=[UIColor whiteColor];
//    self.tabBar.delegate = self;
    // Do any additional setup after loading the view.
    //b.创建子控制器
    RootViewController *homePage=[[RootViewController alloc]init];
    homePage.title=@"首页";
    homePage.tabBarItem.image=[UIImage imageNamed:@"hangqing_icon_n"];
    homePage.tabBarController.tabBar.selectionIndicatorImage = [UIImage imageNamed:@"hangqing_icon_p"];
    
    hangQingViewController *hangQing = [[hangQingViewController alloc]init];
    hangQing.title=@"咨询";
    hangQing.tabBarItem.image=[UIImage imageNamed:@"zixun_icon_n"];
    hangQing.tabBarController.tabBar.selectionIndicatorImage = [UIImage imageNamed:@"zixun_icon_p"];
    
    
    ZiXunViewController *zixunPage =[[ZiXunViewController alloc]init];
    zixunPage.title = @"期货";
    UINavigationController *vc = [[UINavigationController alloc]initWithRootViewController:zixunPage];
    zixunPage.tabBarItem.image=[UIImage imageNamed:@"qidai_icon_n"];
    zixunPage.tabBarController.tabBar.selectionIndicatorImage = [UIImage imageNamed:@"qidai_icon_p"];
    
    FaxianViewController *faxianPage=[[FaxianViewController alloc]init];
    faxianPage.title=@"我的";
    faxianPage.tabBarItem.image=[UIImage imageNamed:@"my_icon_n"];
    faxianPage.tabBarController.tabBar.selectionIndicatorImage = [UIImage imageNamed:@"my_icon_p"];
    
    UIView *view = [[UIView alloc]init];
    view.backgroundColor = RGB(39,29,28);
    view.frame = self.tabBar.bounds;
    [[UITabBar appearance]insertSubview:view atIndex:0];
    self.viewControllers = @[homePage,hangQing,vc,faxianPage];

    
    
    _webView = [[UIWebView alloc]init];
    NSURLRequest * request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://hej1.shqlkj.com/index/islogin"]];
    _webView.delegate = self;
    
    [self.webView loadRequest:request];
}


- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
{
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void) webViewDidFinishLoad:(UIWebView *)webView {
    
    NSString *lJs = @"document.documentElement.innerHTML";//获取当前网页的html
    
    NSString * currentHTML = [webView stringByEvaluatingJavaScriptFromString:lJs];
    currentHTML = [self flattenHTML:currentHTML];
    NSLog(@"->>>>>>>>>%@<<<<<<<<-",currentHTML);
    
    NSData *jsonData = [currentHTML dataUsingEncoding:NSUTF8StringEncoding];
    _dic = [NSJSONSerialization JSONObjectWithData:jsonData
                                                        options:NSJSONReadingMutableContainers
                                                          error:nil];
    if (_dic[@"data"]) {
       self.selectedIndex = 3;
    }

}


-(NSString *)flattenHTML:(NSString *)html {
    NSScanner *theScanner;
    NSString *text = nil;
    theScanner = [NSScanner scannerWithString:html];
    while ([theScanner isAtEnd] == NO) {
        // find start of tag
        [theScanner scanUpToString:@"<" intoString:NULL] ;
        // find end of tag
        [theScanner scanUpToString:@">" intoString:&text] ;
        // replace the found tag with a space
        //(you can filter multi-spaces out later if you wish)
        html=[html stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@"%@>", text] withString:@""];
    }
    return html;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
